package com.springboot42.controller;

import com.springboot42.pojo.Book;
import com.springboot42.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class BookController {
    @Autowired
    BookService bookService;

    @RequestMapping("booklist")//展示书籍信息
    public String getAllBook(Model model){
        model.addAttribute("list",bookService.getAllBook());
        return "booklist.html";
    }

    @RequestMapping("addbook")//转至增加书籍的页面
    public String addbook(){
        return "addbook.html";
    }

    @RequestMapping("addcommit")//确认提交增加书籍，重定向至booklist
    public String addcommit(Book book){
            bookService.addBook(book);
            return "redirect:/booklist";
    }

    @RequestMapping("deleteBookById")
    public String deleteBookById(int id){
        bookService.deleteBookById(id);
        return "redirect:/booklist";
    }

    @RequestMapping("updateBook")
    public String updateBook(Model model,int id,String bookname,int bookprice){
        model.addAttribute("id",id);
        model.addAttribute("bookname",bookname);
        model.addAttribute("bookprice",bookprice);
        return "updatebook.html";
    }

    @RequestMapping("updatecommit")
    public String updatecommit(Book book){
        bookService.updateBook(book);
        return "redirect:/booklist";
    }

}
